function rollDice(value) {

	var rolls = new Array();
	var count = 0;
	var modifier = 0;
	switch(value) {
		case 4:
  			count = document.getElementById('d4Count').value;
  			modifier = document.getElementById('d4Mod').value
 			break;
 		case 6:
  			count = document.getElementById('d6Count').value;
  			modifier = document.getElementById('d6Mod').value
 			break;
 		case 8:
  			count = $('#d8Count').val();
  			modifier = $('#d8Mod').val();
 			break;
 		case 10:
  			count = document.getElementById('d10Count').value;
  			modifier = document.getElementById('d10Mod').value
 			break;
 		case 100:
  			count = document.getElementById('d100Count').value;
  			modifier = document.getElementById('d100Mod').value
			break;
		case 12:
			count = document.getElementById('d12Count').value;
			modifier = document.getElementById('d12Mod').value
			break;
		case 20:
			count = document.getElementById('d20Count').value;
			modifier = document.getElementById('d20Mod').value
			break;
	}
	
	for (i = 0; i < count; i++) {
		rolls[i] = Math.floor((Math.random()*value)+1);
	}
	
	var theRoll = {
  					'guid': guid(),
  					'participantId': gapi.hangout.getParticipantId(),
  					'displayName': document.getElementById('displayName').value,
  					'type': value,
  					'count': count,
  					'rolls': rolls,
  					'modifier': modifier
  				};
	gapi.hangout.data.setValue('newRoll', JSON.stringify(theRoll));
}

function getLocalParticipant() {
	return gapi.hangout.getParticipantById(gapi.hangout.getParticipantId());
}

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function adjustVideoSize() {
	var maxWidth = window.innerWidth - 450;
	var canvas = gapi.hangout.layout.getVideoCanvas();
	var canvasX = canvas.getWidth();
	var canvasY = canvas.getHeight();
	
	if ((maxWidth / canvasX) > (window.innerHeight / canvasY)) {
      	canvas.setHeight(window.innerHeight);
    }
    else {
       	canvas.setWidth(maxWidth);
    }
}

function init() {
  // When API is ready...                                                         
  gapi.hangout.onApiReady.add(
      function(eventObj) {
        if (eventObj.isApiReady) {
	  
	        document.getElementById('displayName').value = getLocalParticipant().person.displayName;
	        
	        adjustVideoSize()
	        gapi.hangout.layout.getVideoCanvas().setPosition(450,0);
	        gapi.hangout.layout.getVideoCanvas().setVisible(true);
	        window.onresize = function() {
							  		adjustVideoSize();
							  };
			gapi.hangout.data.onStateChanged.add( 
				function(event) {
				
					var theRoll = JSON.parse(event.state.newRoll);
					var output = document.getElementById('output');
					
					var who = '';
					if (theRoll.participantId == gapi.hangout.getParticipantId()) {
						who = 'You';
					}
					else {
						who = theRoll.displayName;
					}
					var newRoll = '<span class="roller-name">' + who + '</span> rolled ' + theRoll.count + 'd' + theRoll.type + ' + ' + theRoll.modifier + ':<div style="margin-left: 10px;">';
					var total = 0;
					for (i = 0; i < theRoll.rolls.length; i++) {
						newRoll += theRoll.rolls[i] + ',';
						total += parseInt(theRoll.rolls[i]);
					}
					if (theRoll.modifier >= 0) {
						newRoll += '+';
					}
					total += parseInt(theRoll.modifier);
					newRoll += theRoll.modifier + '<br />Total: ' + total;
					$('<li>' + newRoll + '</div></li>').appendTo('#output').hide().fadeIn(800); 
					//$('#outputContainer').scrollTop($('#outputContainer')[0].scrollHeight);
					$('#outputContainer').animate({scrollTop:$('#outputContainer')[0].scrollHeight},800);
				}
			);
        }
      });
}